import { Component, OnInit, Input, Injector } from '@angular/core';
import { HttpClient, HttpRequest, HttpResponse } from "@angular/common/http";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { debounceTime } from 'rxjs/operators/debounceTime';
import { map } from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
import { ActivatedRoute, NavigationExtras } from '@angular/router';
import { NzMessageService, NzModalRef, NzModalService, UploadFile } from 'ng-zorro-antd';
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { environment } from 'environments/environment';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { StudentJoinCourseOutput } from '../../../core/model/StudentJoinCourseDto';
import { isNumber } from 'util';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-mark-inquiry',
  templateUrl: './mark-inquiry.component.html',
  styleUrls: ['../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class MarkInquiryComponent implements OnInit {
  isLoading = false;
  courseArr = new Array();
  classArr = new Array();

  markArr;
  //
  currentPage = 1;
  pageSize = 10;
  recordTotalCount = 0;
  entities = new Array();

  markexcel;
  markexcelid = "markexcelid";
  //
  _coursePicked;
  get coursePicked() {
    return this._coursePicked;
  }
  set coursePicked(value) {
    this._coursePicked = value;
    this.iniClass();
  }
  //
  _classPicked;
  get classPicked() {
    return this._classPicked;
  }
  set classPicked(value) {
    this._classPicked = value;
    this.iniMark();
  }
  //
  constructor(injecter: Injector,
    private modal: NzModalService,
    public httpService: HttpService,
    public http: HttpClient) {
    this.inifun();
  }
  async inifun() {
    const courses = await this.httpService.get<any>('Course/GetCourses');
    this.courseArr = new Array();
    if (courses.length !== 0) {
      this.courseArr = courses;
      this._coursePicked = this.courseArr[0].dValue;
      this.iniClass();
    }
  }

  async iniClass() {
    const classes = await this.httpService.get<any>('Course/GetCourseClasses?courseId=' + this._coursePicked);
    this.classArr = new Array();
    if (classes.length !== 0) {
      this.classArr = classes;
      this._classPicked = this.classArr[0].dValue;
      this.iniMark();
    }
  }
  async iniMark() {
    
    try {
      this.isLoading = true;
      const marks = await this.httpService.get<any>(
        `Course/GetMarks?courseId=${this._coursePicked}&classId=
        ${this._classPicked}&currentPage=${this.currentPage}&pageSize=${this.pageSize}`);
      this.markArr = marks;
      this.recordTotalCount = this.markArr.recordTotalCount;
      this.currentPage = this.markArr.currentPageIndex;
      this.pageSize = this.markArr.pageSize;
      //
      this.entities = new Array();
      if (marks.entities.length !== 0) {
        this.entities = marks.entities;
      }
    } finally {
      
      this.isLoading = false;
    }
  }
  async getEntityListAsync(input: any) {
    this.iniMark();
  }
  async toExport(input: any) {
    const par = { CourseId: parseInt(this._coursePicked, 10), ClassId: parseInt(this._classPicked, 10) };
    const result = await this.httpService.post<any>('Course/ExportMarks', par);
    this.markexcel = result;
    window.open(this.httpService.IP_PORT + result);
  }
  ngOnInit() {

  }
}
